package com.yanghui.dtracing.plugin.hystrix;


import com.yanghui.dtracing.core.EnhancedInstance;
import com.yanghui.dtracing.core.opentracing.ScopeContext;
import com.yanghui.dtracing.core.opentracing.Tracing;
import com.yanghui.dtracing.core.plugin.interceptor.IInstanceMethodAroundInterceptor;
import com.yanghui.dtracing.core.plugin.interceptor.MethodInterceptResult;

import java.lang.reflect.Method;

public class HystrixInstanceMethodAroundInterceptor implements IInstanceMethodAroundInterceptor /*extends AbstractInstanceMethodInterceptor*/ {

    @Override
    public void beforeMethod(Object thisObj, Method method, Object[] allArguments, MethodInterceptResult result) throws Throwable {
        if (!(thisObj instanceof EnhancedInstance)) {
            return;
        }
        EnhancedInstance enhancedInstance = (EnhancedInstance)thisObj;
        HystrixAcross hystrixAcross = (HystrixAcross)enhancedInstance.getDynamicField();

        if(hystrixAcross.getSpan() != null){
            Tracing.getTracer().activateSpan(hystrixAcross.getSpan());
        }
        if(hystrixAcross.getAtomicInteger() != null){
            ScopeContext.putIncrement(hystrixAcross.getAtomicInteger());
        }
    }
}
